WYZE_ACCESSORY_VERSION = 1.0
WYZE_ACCESSORY_SITE_METHOD = local
WYZE_ACCESSORY_SITE = $(WYZE_ACCESSORY_PKGDIR)/files

define WYZE_ACCESSORY_INSTALL_TARGET_CMDS_DOORBELL_CTRL
	$(TARGET_CC) $(TARGET_CFLAGS) -o $(TARGET_DIR)/usr/sbin/doorbell_ctrl $(WYZE_ACCESSORY_PKGDIR)/files/doorbell_chime.c $(TARGET_LDFLAGS)
endef

define WYZE_ACCESSORY_INSTALL_DOORBELL_BUTTON_CONF
	$(INSTALL) -m 0755 -d $(TARGET_DIR)/etc
	echo -e "KEY_1 RELEASE 0 doorbell_ctrl $(BR2_PACKAGE_WYZE_ACCESSORY_DOORBELL_CTRL_MAC) 15 1\nKEY_1 TIMED 0.1 play /usr/share/sounds/th-doorbell_3.opus" \
		>> $(TARGET_DIR)/etc/thingino-button.conf
	#echo -e "61 high doorbell chime" >> $(TARGET_DIR)/etc/gpio.conf
endef

define WYZE_ACCESSORY_INSTALL_TARGET_CMDS_FLOODLIGHT
	$(INSTALL) -m 0755 -d $(TARGET_DIR)/etc/modules.d
	echo ch341 >> $(TARGET_DIR)/etc/modules.d/accessory
	echo snd-usb-audio >> $(TARGET_DIR)/etc/modules.d/accessory
endef

define WYZE_ACCESSORY_INSTALL_TARGET_CMDS_SPOTLIGHT
	$(INSTALL) -D -m 0755 $(WYZE_ACCESSORY_PKGDIR)/files/spotlight_ctl \
		$(TARGET_DIR)/usr/sbin/spotlight_ctl

	$(INSTALL) -m 0755 -d $(TARGET_DIR)/etc/modules.d
	echo ch341 >> $(TARGET_DIR)/etc/modules.d/accessory
endef

define WYZE_ACCESSORY_INSTALL_TARGET_CMDS_CAR
	$(INSTALL) -D -m 0755 $(WYZE_ACCESSORY_PKGDIR)/files/car_control \
		$(TARGET_DIR)/usr/sbin/car_control

	$(INSTALL) -m 0755 -d $(TARGET_DIR)/etc/modules.d
	echo cp210x >> $(TARGET_DIR)/etc/modules.d/accessory
endef

define WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS_FLOODLIGHT
	$(call KCONFIG_SET_OPT,CONFIG_USB_SERIAL,m)
	$(call KCONFIG_SET_OPT,CONFIG_USB_SERIAL_CH341,m)
	$(call KCONFIG_ENABLE_OPT,CONFIG_SND)
	$(call KCONFIG_ENABLE_OPT,CONFIG_SND_USB)
	$(call KCONFIG_ENABLE_OPT,CONFIG_USB_DWC2_FULLSPEED_HOST)
	$(call KCONFIG_SET_OPT,CONFIG_SND_USB_AUDIO,m)
endef

define WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS_SPOTLIGHT
	$(call KCONFIG_SET_OPT,CONFIG_USB_SERIAL,m)
	$(call KCONFIG_SET_OPT,CONFIG_USB_SERIAL_CH341,m)
endef

define WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS_CAR
	$(call KCONFIG_SET_OPT,CONFIG_USB_SERIAL,m)
	$(call KCONFIG_SET_OPT,CONFIG_USB_SERIAL_CP210X,m)
endef

ifeq ($(BR2_PACKAGE_WYZE_ACCESSORY_FLOODLIGHT),y)
	WYZE_ACCESSORY_INSTALL_TARGET_CMDS += $(WYZE_ACCESSORY_INSTALL_TARGET_CMDS_FLOODLIGHT)
	WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS += $(WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS_FLOODLIGHT)
endif

ifeq ($(BR2_PACKAGE_WYZE_ACCESSORY_SPOTLIGHT),y)
	WYZE_ACCESSORY_INSTALL_TARGET_CMDS += $(WYZE_ACCESSORY_INSTALL_TARGET_CMDS_SPOTLIGHT)
	WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS += $(WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS_SPOTLIGHT)
endif

ifeq ($(BR2_PACKAGE_WYZE_ACCESSORY_CAR),y)
	WYZE_ACCESSORY_INSTALL_TARGET_CMDS += $(WYZE_ACCESSORY_INSTALL_TARGET_CMDS_CAR)
	WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS += $(WYZE_ACCESSORY_LINUX_CONFIG_FIXUPS_CAR)
endif

ifeq ($(BR2_PACKAGE_WYZE_ACCESSORY_DOORBELL_CTRL),y)
	WYZE_ACCESSORY_INSTALL_TARGET_CMDS += $(WYZE_ACCESSORY_INSTALL_TARGET_CMDS_DOORBELL_CTRL)
	WYZE_ACCESSORY_TARGET_FINALIZE_HOOKS += WYZE_ACCESSORY_INSTALL_DOORBELL_BUTTON_CONF
endif

define WYZE_ACCESSORY_INSTALL_CMDS
	$(WYZE_ACCESSORY_INSTALL_TARGET_CMDS)
endef

$(eval $(generic-package))

define WYZE_ACCESSORY_INSTALL_TARGET_CMDS
	$(call WYZE_ACCESSORY_INSTALL_TARGET_CMDS_FLOODLIGHT)
	$(call WYZE_ACCESSORY_INSTALL_TARGET_CMDS_SPOTLIGHT)
	$(call WYZE_ACCESSORY_INSTALL_TARGET_CMDS_CAR)
	$(call WYZE_ACCESSORY_INSTALL_TARGET_CMDS_DOORBELL_CTRL)
endef
